const express = require("express");
const fs = require("fs");
// const { request } = require("http");
// const { url } = require("inspector");
const pool = require("./pool.js");
const querystring = require('querystring');
let router = express.Router();
//响应html页面
router.get("/:html", (request, response, next) => {
    response.setHeader("Access-Control-Allow-Origin", "*");
    //异步读取数据
    let read = fs.readFile(`./public/${request.params.html}.html`, 'utf-8', (err, data) => {
        if (err) {
            return;
        } else {
            //返回html页面给客户端
            //设置响应头
            response.writeHead(200, {
                'Content-Type': "text/html;'charset=utf-8'"
            })
            response.end(data);
        }
    })
});
//响应css文件
router.get("/css/:name", (request, response, next) => {
    // console.log(request.params.name);
    //设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //异步读取数据
    let read = fs.readFile(`./public/css/${request.params.name}`, 'utf-8', (err, data) => {
        if (err) {
            return;
        } else {
            //设置响应头
            response.writeHead(200, {
                'Content-Type': "text/css;'charset=utf-8'",
            });
            // console.log(data.toString());
            response.end(data);

        }
    })
});
//响应js文件
router.get("/js/:name", (request, response, next) => {
    // console.log(request.params.name);
    //设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //异步读取数据
    let read = fs.readFile(`./public/js/${request.params.name}`, 'utf-8', (err, data) => {
        if (err) {
            return;
        } else {
            //设置响应头
            response.writeHead(200, {
                'Content-Type': "text/javascript;'charset=utf-8'",
            });
            console.log(data.toString());
            response.end(data);
        }
    })
});
//响应img数据
router.get("/img/:name", (request, response, next) => {
    // console.log(request.params.name);
    //设置允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    //异步读取数据
    let read = fs.readFile(`./public/img/${request.params.name}`, (err, data) => {
        if (err) {
            return;
        } else {
            //设置响应头
            response.writeHead(200, {
                'Content-Type': 'image/jpg'
            });
            // console.log(data.toString());
            response.end(data);
        }
    })
});
//返回分类查询数据
router.get("/detail/search", (resquest, response, next) => {
    // console.log(resquest.query);
    pool.query('select * from user', (err, result) => {
        if (err) {
            throw err;
            return;
        }
    })
    response.writeHead(200, {
        msg: 'ok'
    });
    response.end();
})
//登陆
router.post('/login', (req, res, next) => {
    // let msg1=null;
    req.on('data', (chunck) => {
        let obj = querystring.parse(chunck.toString());
        pool.query('select pwd from user where pwd = ? & p_id = ?', [obj.pwd,obj.pid], (err, result) => {
            if (err) {
                throw err;
                return;
            } else {
                if (result.length > 0) {
                    res.status(200).send({
                        msg: 'ok'
                    });
                } else {
                    res.status(400).send({
                        msg: "no"
                    });
                }
            }
        })
    })
    // console.log(msg1.pwd);

})
//注册
router.post('/resign',(req,res,next)=>{
    req.on('data', (chunck) => {
        let obj = querystring.parse(chunck.toString());
        pool.query('inert into user values(null,null,?,?,?,md5(?)', [obj.nickName,obj.isVip,obj.Date,obj.pwd], (err, result) => {
            if (err) {
                //返回，后抛出异常
                res.status(400).send({
                    msg:-2
                })
                throw err;
                return;
            } else {
                if (result.length > 0) {
                    res.status(200).send({
                        msg: 1
                    });
                } else {
                    res.status(401).send({
                        msg: -1
                    });
                }
            }
        })
    })
})
router.get('/search',(req,res,next)=>{
    
})
































module.exports = router;